package view
{
	import assets.view.components.PopupViewSymbol;
	
	import flash.display.BlendMode;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextFieldAutoSize;
	
	import model.vo.ViewVO;
	
	import org.osmf.net.StreamingURLResource;
	import org.puremvc.as3.core.View;
	
	import view.components.GeneralButtonView;

	public class PopupView extends PopupViewSymbol
	{
		public static const NAME:String 			= "PopupView";
		public static const SHOW:String				= NAME + "Show";
		
		private var viewData:ViewVO;
		
		private var stageWidth:Number;
		private var stageHeight:Number;
		private var pos:Point;
		
		private var okBtn:GeneralButtonView;
		
		public function PopupView()
		{
			this.visible = false;
		}
		
		public function init( viewVO:ViewVO ):void
		{
			viewData 		= viewVO;
			
			stageWidth 		= ApplicationFacade.APP_WIDTH;
			stageHeight 	= ApplicationFacade.APP_HEIGHT;
			
			setPopupViewProperties();
			buildPopupView();
		}
		
		public function show( val:Object ):void
		{
			this.visible = true;
			
			headerLabel.text 			= val.headerTxt;
			headerLabelDetail.text 		= val.headerDetailTxt;
			labelInfo.text 				= val.info;
			labelInfoDetail.text 		= val.infoDetail;
			
			labelsContainer.x = Math.round(  popupContainer.x - (viewData.POPUP_WIDTH / 2) );
			labelsContainer.y = Math.round(  popupContainer.y - (viewData.POPUP_HEIGHT / 2) );
			
			FX.fadeIn( overlayContainer );
			FX.fadeIn( labelsContainer );
			FX.scaleIn( popupContainer, scaleInCompleteHandler, scaleInProgressHandler );
		}
		
		public function hide():void
		{			
			bckgrdContainer.filters = [];
			
			FX.fadeOut( overlayContainer );
			labelsContainer.alpha = 0;
			FX.scaleOut( popupContainer, scaleOutCompleteHandler, scaleInProgressHandler );
		}
		
		private function setPopupViewProperties():void
		{
			headerLabel.autoSize 			= TextFieldAutoSize.LEFT;
			headerLabel.textColor 			= viewData.labelSelectedColor;
			headerLabel.alpha 				= 0;
//			headerLabel.border 				= true;
			
			headerLabelDetail.autoSize 		= TextFieldAutoSize.LEFT;
			headerLabelDetail.textColor 	= viewData.labelSelectedColor;
			headerLabelDetail.alpha 		= 0;
//			headerLabelDetail.border 		= true;
			
			labelInfo.textColor 			= viewData.labelColor;
			labelInfo.alpha 				= 0;
//			labelInfo.border 				= true;
			labelInfoDetail.textColor 		= viewData.labelColor;
			labelInfoDetail.alpha 			= 0;
//			labelInfoDetail.border 			= true;
		}
		
		private function buildPopupView():void
		{
			paintBckgrd();
			paintHeader();
			paintFrame();
			paintOverlay();
			
			popupContainer.addChild( bckgrdContainer );
			popupContainer.addChild( headerContainer );
			popupContainer.addChild( btnsContainer );
			popupContainer.addChild( frameContainer );
			
			labelsContainer.addChild( labelInfo );
			labelsContainer.addChild( labelInfoDetail );
			labelsContainer.addChild( headerLabel );
			labelsContainer.addChild( headerLabelDetail );
			
			labelsContainer.alpha = 0;
			
			okBtn = new GeneralButtonView();
			okBtn.addEventListener( GeneralButtonView.CLICKED, okBtnClickedHandler);
			okBtn.init( viewData, "OK");
			btnsContainer.addChild( okBtn );
			btnsContainer.x = Math.round( popupContainer.width - btnsContainer.width - 10);
			btnsContainer.y = Math.round( popupContainer.height - btnsContainer.height - 10);
			
			popupContainer.scaleX = popupContainer.scaleY = 0;
			
			pos = new Point( (stageWidth / 2 ) - (popupContainer.width / 2),
				(stageHeight / 2 ) - (popupContainer.height / 2) );
			popupContainer.x = Math.round( pos.x );
			popupContainer.y = Math.round( pos.y );
			
			labelsContainer.x = popupContainer.x;
			labelsContainer.y = popupContainer.y;
		}
		
		private function paintBckgrd():void
		{
			var g:Graphics = bckgrdContainer.graphics;
			g.clear();
			g.beginFill( viewData.POPUP_BCKGRD_COLOR );
			g.drawRect( 0, 0, viewData.POPUP_WIDTH, viewData.POPUP_HEIGHT );
			g.endFill();
		}
		
		private function paintFrame():void
		{
			var g:Graphics = frameContainer.graphics;
			g.clear();
			g.lineStyle( viewData.LINE_WEIGHT, viewData.LINE_COLOR );
			g.drawRect( 0, 0, viewData.POPUP_WIDTH, viewData.POPUP_HEIGHT );
		}
		
		private function paintHeader():void
		{
			var g:Graphics = headerContainer.graphics;
			g.clear();
			g.beginFill( viewData.POPUP_HEADER_BCKGRD_COLOR );
			g.drawRect( 0, 0, viewData.POPUP_WIDTH, viewData.POPUP_HEADER_HEIGHT );
			g.endFill();
		}
		
		private function paintOverlay():void
		{
			overlayContainer.alpha = 0;
			
			var colors:Array 				= [ 0xFFFFFF, 0x000000 ];
			var alphas:Array 				= [ 0.8, 0.5 ];
			var ratios:Array 				= [ 0, 255 ];
			var gradientMatrix:Matrix 		= new Matrix();
			var gradientMatRot:Number 		= 1.570796327; //90 degrees
			
			gradientMatrix.createGradientBox( stageWidth, stageHeight, gradientMatRot, 0, 0 );
			
			var g:Graphics = overlayContainer.graphics;
			g.clear();
			g.beginGradientFill( GradientType.RADIAL, colors, alphas, ratios, gradientMatrix );
			g.drawRect( 0, 0, stageWidth, stageHeight );
			g.endFill();
		}
		
		private function okBtnClickedHandler( e:Event ):void
		{
			hide();
		}
		
		private function scaleInCompleteHandler():void
		{
			FX.fadeIn( headerLabel );
			FX.fadeIn( headerLabelDetail );
			
			FX.fadeIn( labelInfo );
			FX.fadeIn( labelInfoDetail );
			
			bckgrdContainer.filters = [ new DropShadowFilter() ];
		}
		
		private function scaleInProgressHandler():void
		{
			pos = new Point( (stageWidth / 2 ) - (popupContainer.width / 2),
				(stageHeight / 2 ) - (popupContainer.height / 2) );
			popupContainer.x = Math.round( pos.x );
			popupContainer.y = Math.round( pos.y );
		}
		
		private function scaleOutCompleteHandler():void
		{
			okBtn.reset();
			popupContainer.alpha = 1;
			this.visible = false;
		}
	}
}